package com.huike.pmps.service.impl;

import com.huike.pmps.dao.CockpitRegionDao;
import com.huike.pmps.dao.NewSignDao;
import com.huike.pmps.dao.PaymentCollectionDao;
import com.huike.pmps.model.entity.Member;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.CockpitRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@Service
public class CockpitRegionServiceImpl implements CockpitRegionService {
    @Autowired
    NewSignDao newSignDao;
    @Autowired
    PaymentCollectionDao paymentCollectionDao;
    @Autowired
    CockpitRegionDao cockpitRegionDao;

    /**
     * 战区 新合同数量及金额
     *
     * @return
     */
    @Override
    public Result selectNewSignData(Member member, Integer year, Integer month) {
        //战区司令
        if (!member.getRolesIds().contains(2)) return Result.failure("您的账号不是战区司令角色，无权查看");
        if (member.getRegionId() == null) return Result.failure("您的账号没有配置所属战区,请联系系统管理员进行战区配置");
        if (year != null && month != null && month == 0) {
            return Result.success(newSignDao.selectRegionNewSignData(member.getRegionId(), year, null));
        } else if (year != null && month != null && month != 0) {
            return Result.success(newSignDao.selectRegionNewSignData(member.getRegionId(), year, month));
        } else {
            return Result.success(newSignDao.selectRegionNewSignData(member.getRegionId(), LocalDate.now().getYear(), LocalDate.now().getMonthValue()));
        }
    }

    /**
     * 战区 产品销售金额及数量
     */
    @Override
    public Result selectProductNewSignContractAmount(Member member, String provinceName, Integer year, Integer month) {
        //战区司令
        if (!member.getRolesIds().contains(2)) return Result.failure("您的账号不是战区司令角色，无权查看");
        if (member.getRegionId() == null) return Result.failure("您的账号没有配置所属战区,请联系系统管理员进行战区配置");
        return Result.success(newSignDao.selectRegionProductNewSignContractAmount(member.getRegionId(), provinceName, year, month));
    }

    /**
     * 战区 周期款情况
     */
    @Override
    public Result selectPaymentCollectAmount(Member member, Integer year) {
        //战区司令
        if (!member.getRolesIds().contains(2)) return Result.failure("您的账号不是战区司令角色，无权查看");
        if (member.getRegionId() == null) return Result.failure("您的账号没有配置所属战区,请联系系统管理员进行战区配置");
        return Result.success(paymentCollectionDao.selectRegionPaymentCollectAmount(member.getRegionId(), year));
    }

    /**
     * 战区 运营毛利
     */
    @Override
    public Result selectGrossProfit(Member member, Integer year, Integer month) {
        //战区司令
        if (!member.getRolesIds().contains(2)) return Result.failure("您的账号不是战区司令角色，无权查看");
        if (member.getRegionId() == null) return Result.failure("您的账号没有配置所属战区,请联系系统管理员进行战区配置");
        if (year != null && month != null && month == 0) {
            return Result.success(cockpitRegionDao.getGrossProfitByYear(member.getRegionId(), year));
        } else if (year != null && month != null && month != 0) {
            return Result.success(cockpitRegionDao.getGrossProfitByMonth(member.getRegionId(), year, month));
        } else {
            return Result.success(cockpitRegionDao.getGrossProfitByMonth(member.getRegionId(), LocalDate.now().getYear(), LocalDate.now().getMonthValue()));
        }

    }


    /**
     * 战区 运营财务数据
     */
    @Override
    public Result selectFinance(Member member, Integer year, Integer month) {
        //战区司令
        if (!member.getRolesIds().contains(2)) return Result.failure("您的账号不是战区司令角色，无权查看");
        if (member.getRegionId() == null) return Result.failure("您的账号没有配置所属战区,请联系系统管理员进行战区配置");
        if (year != null && month != null && month == 0) {
            return Result.success(cockpitRegionDao.getFinanceByYear(member.getRegionId(), year));
        } else if (year != null && month != null && month != 0) {
            return Result.success(cockpitRegionDao.getFinanceByMonth(member.getRegionId(), year, month));
        } else {
            return Result.success(cockpitRegionDao.getFinanceByMonth(member.getRegionId(), LocalDate.now().getYear(), LocalDate.now().getMonthValue()));
        }
    }

    @Override
    public Result selectBusinessOpportunities(Member member, Integer year, Integer month) {
        //战区司令
        if (!member.getRolesIds().contains(2)) return Result.failure("您的账号不是战区司令角色，无权查看");
        if (member.getRegionId() == null) return Result.failure("您的账号没有配置所属战区,请联系系统管理员进行战区配置");
        if (year != null && month != null && month == 0) {
            return Result.success(cockpitRegionDao.getBusinessOpportunities(member.getRegionId(), year, null));
        } else if (year != null && month != null && month != 0) {
            return Result.success(cockpitRegionDao.getBusinessOpportunities(member.getRegionId(), year, month));
        } else {
            return Result.success(cockpitRegionDao.getBusinessOpportunities(member.getRegionId(), LocalDate.now().getYear(), LocalDate.now().getMonthValue()));
        }
    }

    @Override
    public Result selectBusinessOpportunitiesStage30(Member member, Integer year, Integer month) {
        //战区司令
        if (!member.getRolesIds().contains(2)) return Result.failure("您的账号不是战区司令角色，无权查看");
        if (member.getRegionId() == null) return Result.failure("您的账号没有配置所属战区,请联系系统管理员进行战区配置");
        if (year != null && month != null && month == 0) {
            return Result.success(cockpitRegionDao.getBusinessOpportunitiesStage30(member.getRegionId(), year, null));
        } else if (year != null && month != null && month != 0) {
            return Result.success(cockpitRegionDao.getBusinessOpportunitiesStage30(member.getRegionId(), year, month));
        } else {
            return Result.success(cockpitRegionDao.getBusinessOpportunitiesStage30(member.getRegionId(), LocalDate.now().getYear(), LocalDate.now().getMonthValue()));
        }
    }

    @Override
    public Result selectOnLineCourseStock(Member member) {
        //战区司令
        if (!member.getRolesIds().contains(2)) return Result.failure("您的账号不是战区司令角色，无权查看");
        if (member.getRegionId() == null) return Result.failure("您的账号没有配置所属战区,请联系系统管理员进行战区配置");
        return Result.success(Result.success(cockpitRegionDao.getOnLineCourseStock(member.getRegionId())));
    }

    @Override
    public Result selectCourseCapacity(Member member) {
        //战区司令
        if (!member.getRolesIds().contains(2)) return Result.failure("您的账号不是战区司令角色，无权查看");
        if (member.getRegionId() == null) return Result.failure("您的账号没有配置所属战区,请联系系统管理员进行战区配置");
        return Result.success(Result.success(cockpitRegionDao.getCourseCapacity(member.getRegionId())));
    }
}
